package cn.ecut.controller;


import cn.ecut.model.Fangjian;
import cn.ecut.model.Message;
import cn.ecut.model.Puwei;
import cn.ecut.service.PuweiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.Property;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("/puwei")
public class PuweiController {
    @Autowired
    private PuweiService puweiService;

    @ResponseBody
    @RequestMapping("/show")
    public HashMap show() {
        HashMap<String, Object> map = new HashMap<String, Object> ();
        Integer count = puweiService.selectAllcount();
        List<Puwei> list = new ArrayList<>();
        list=puweiService.selectAll();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        return map;
    }

    @ResponseBody
    @RequestMapping("/add")
    public Message add(Puwei puwei) {
        Message message = new Message();
        Fangjian fangjian = new Fangjian();
        fangjian = puweiService.selectFangjian(puwei.getFangjianbianma());
        puwei.setFangjian(fangjian.getFangjian());
        puwei.setPuweibianhao(puweiService.getpuweiBianhao());
        if (puweiService.selectAllcountPuwei(puwei.getFangjianbianma()) < fangjian.getPuweishu()) {
            puwei.setShiyongzhuangtai(1);
            fangjian.setShiyongzhuangtai(2);
            puweiService.updateShiyongzhuangtai(fangjian);
            int n = puweiService.insert(puwei);
            if (n == 1) {
                message.setFlag(true);
                return message;
            } else {
                message.setFlag(false);
                return message;
            }
        } else {
            fangjian.setShifouyoupuwei(0);
            puweiService.updateShifouyoupuwei(fangjian);
            message.setFlag(false);
            message.setData("房间已满");
            return message;
        }


    }

    @ResponseBody
    @RequestMapping("/select")
    public HashMap select(String searchResult){
        HashMap<String, Object> map = new HashMap<String, Object> ();
        List<Puwei> list = new ArrayList<>();
        list=puweiService.advanceSelect(searchResult);
        Integer count = list.size();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        return map;
    }

    @ResponseBody
    @RequestMapping("/delete")
    public Message delete(String puweibianhao){
        Message message = new Message();
        Fangjian fangjian = new Fangjian();
        fangjian = puweiService.selectFangjian(puweiService.selectFangjianbianma(puweibianhao));
        int n = puweiService.deleteByPrimaryKey(puweibianhao);
        if(n == 1){
            message.setFlag(true);
            fangjian.setShifouyoupuwei(1);
            puweiService.updateShifouyoupuwei(fangjian);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/qiyong")
    public Message qiyong(Puwei puwei){
        Message message =new Message();
        if(puwei.getQiyongzhuangtai()==0){
            puwei.setQiyongzhuangtai(1);
        }else {
            puwei.setQiyongzhuangtai(0);
        }
        int n = puweiService.updateQiyong(puwei);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/selectAllfangjian")
    public Message selectAllfangjian(){
        Message message = new Message();
        List<Fangjian> list = new ArrayList<>();
        list = puweiService.selectAllfangjian();
        Iterator<Fangjian> iterator = list.iterator();
        while (iterator.hasNext()){
            Fangjian fangjian = iterator.next();
            if(fangjian.getQiyongzhuangtai() == 0){
                iterator.remove();
            }
        }
        message.setFlag(true);
        message.setData(list);
        return message;
    }

    @ResponseBody
    @RequestMapping("/update")
    public Message update(Puwei puwei) {
        Message message = new Message();
        int n = puweiService.updateByPrimaryKey(puwei);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }
}
